import React from "react";
import { Tree, Button, message } from "antd";
import styles from "./robotAdd.less";
import { getTemplateTreeList, setTemplateTreeList } from '@/services/modelInstruction';
import { agentShare, agentShareList } from "@/services/assistantCenter";
import { getAssistantShareList } from '@/services/digitalEmployees';
const { TreeNode } = Tree;
import { formatMessage } from 'umi-plugin-react/locale';

class AssistantList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            agentTree: {
                list: [],
                expandedKeys: [],
                checkedKeys: [],
                selectedKeys: [],
                agentIds: [],
                categoryIds: []
            },
            digitalEmployeesTree: {
                list: [],
                expandedKeys: [],
                checkedKeys: [],
                selectedKeys: [],
            }
        };
    }

    componentDidMount() {
        if (this.props.onRef) {
            this.props.onRef(this);
        }
    }

    getList = async () => {
        this.getAgentTree();
        this.getDigitalEmployees();
    }

    getAgentTree = async () => {
        const { agentTree } = this.state;
        const { applicationId } = this.props;
        let res = await agentShareList({ applicationId });
        console.log(res);
        let list = [], checkedFileIds = [], categoryIds = [], agentIds = [];
        list[0] = res;
        function getChildren(data) {
            data.map((item) => {
                // console.log(item);
                if (item.children && item.children.length > 0) {
                    getChildren(item.children);
                };
                if (item.check) {
                    checkedFileIds.push(item.id);
                };
                if (item.isCategory) {
                    categoryIds.push(item.id);
                } else {
                    agentIds.push(item.id);
                }
            });
        };

        if (list.length) {
            getChildren(list);
        };

        this.setState({
            agentTree: { ...agentTree, list, checkedKeys: checkedFileIds, agentIds, expandedKeys: categoryIds, categoryIds },
        })
    }

    getDigitalEmployees = async () => {
        const { digitalEmployeesTree } = this.state;
        const { applicationId } = this.props;
        let res = await getAssistantShareList({ applicationId });
        console.log(res);
        let list = [], checkedFileIds = [], categoryIds = [], agentIds = [];
        list[0] = res;
        function getChildren(data) {
            data.map((item) => {
                // console.log(item);
                if (item.children && item.children.length > 0) {
                    getChildren(item.children);
                };
                if (item.check) {
                    checkedFileIds.push(item.id);
                };
            });
        };

        if (list.length) {
            getChildren(list);
        };

        this.setState({
            digitalEmployeesTree: { ...digitalEmployeesTree, list, checkedKeys: checkedFileIds, expandedKeys: ["all"] },
        })
    }

    onSubmit = async () => {
        const { applicationId } = this.props;
        const { agentTree, digitalEmployeesTree } = this.state;
        let obj = {
            applicationId,
            agentIds: agentTree.checkedKeys.filter(k => agentTree.agentIds.indexOf(k) > -1),
            categoryIds: agentTree.checkedKeys.filter(k => agentTree.categoryIds.indexOf(k) > -1),
            assistantIds: digitalEmployeesTree.checkedKeys,
        };
        let res = await agentShare(obj);
        console.log(res);
        if (res.code === 0) {
            message.success('操作成功');
            this.getList();
        }
    };

    onExpand1 = (expandedKeys) => {
        const { agentTree } = this.state;
        this.setState({
            agentTree: { ...agentTree, expandedKeys, autoExpandParent: false },
        });
    };

    onCheck1 = (checkedKeys) => {
        const { agentTree } = this.state;
        this.setState({
            agentTree: { ...agentTree, checkedKeys },
        });
    };

    renderTreeNodes1 = (data) =>
        data.map((item) => {
            if (item.children) {
                return (
                    <TreeNode title={item.name} key={item.id} dataRef={item}>
                        {this.renderTreeNodes1(item.children)}
                    </TreeNode>
                );
            }
            return <TreeNode key={item.id} title={item.name} {...item} />;
        });

    onExpand2 = (expandedKeys) => {
        const { digitalEmployeesTree } = this.state;
        this.setState({
            digitalEmployeesTree: { ...digitalEmployeesTree, expandedKeys, autoExpandParent: false },
        });
    };

    onCheck2 = (checkedKeys) => {
        const { digitalEmployeesTree } = this.state;
        this.setState({
            digitalEmployeesTree: { ...digitalEmployeesTree, checkedKeys },
        });
    };

    renderTreeNodes2 = (data) =>
        data.map((item) => {
            if (item.children) {
                return (
                    <TreeNode title={item.name} key={item.id} dataRef={item}>
                        {this.renderTreeNodes2(item.children)}
                    </TreeNode>
                );
            }
            return <TreeNode key={item.id} title={item.name} {...item} />;
        });

    render() {
        const { agentTree, digitalEmployeesTree } = this.state;

        return (
            <div style={{ padding: "10px 10px 10px 30px" }}>
                <div style={{ display: 'flex', gap: '12px', }}>
                    {
                    //   <div style={{ width: '40%' }}>
                    //     <div style={{ fontWeight: 'bold' }}>Agent助手</div>
                    //     <div style={{ maxHeight: '54vh', minHeight: '120px', marginBottom: 20, overflowY: 'auto', display: 'inline-flex' }}>
                    //         <Tree
                    //             checkable
                    //             onExpand={this.onExpand1}
                    //             expandedKeys={agentTree.expandedKeys}
                    //             autoExpandParent={true}
                    //             onCheck={this.onCheck1}
                    //             checkedKeys={agentTree.checkedKeys}
                    //             selectable={false}
                    //         >
                    //             {this.renderTreeNodes1(agentTree.list)}
                    //         </Tree>
                    //     </div>
                    // </div>
                    }
                    <div style={{ flex: 1 }}>
                        <div style={{ fontWeight: 'bold' }}>{formatMessage({ id: 'menu.assistantCenter' })}</div>
                        <div style={{ maxHeight: '54vh', minHeight: '120px', marginBottom: 20, overflowY: 'auto', display: 'inline-flex' }}>
                            <Tree
                                checkable
                                onExpand={this.onExpand2}
                                expandedKeys={digitalEmployeesTree.expandedKeys}
                                autoExpandParent={true}
                                onCheck={this.onCheck2}
                                checkedKeys={digitalEmployeesTree.checkedKeys}
                                selectable={false}
                            >
                                {this.renderTreeNodes2(digitalEmployeesTree.list)}
                            </Tree>
                        </div>
                    </div>
                </div>

                <Button type="primary" onClick={this.onSubmit} style={{ display: 'block' }}>{formatMessage({ id: 'robotAdd.confirm' })}</Button>
            </div>
        );
    }
}

export default AssistantList;
